<template>
  <chart :options="options" :auto-resize="autoResize" :theme="theme" :watch-shallow="watchShallow"
      :class="className" :style="{height:height,width:width}"></chart>
</template>

<script>

const animationDuration = 2000

export default {
  props: {
    chartData: { type: Object },
    className: { type: String, default: 'chart' },
    theme: { type: String, default: 'macarons' },
    autoResize: { type: Boolean, default: true },
    watchShallow: { type: Boolean, default: true },
    width: { type: String, default: '100%' },
    height: { type: String, default: '230px' }
  },
  data() {
    return {
      options: this.buildOptions(this.charData)
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.options = this.buildOptions(val)
      }
    }
  },
  mounted() {
    if (!this.options && this.chartData) {
      this.options = this.buildOptions(this.chartData)
    }
  },
  methods: {
    buildOptions(chartData) {
      var _this = this
      if (chartData === null) {
        chartData = {}
        chartData.category = []
        chartData.valueBeans = []
        chartData.originalDatas = []
      }
      if (!chartData || !chartData.valueBeans) {
        return
      }
      var category = chartData.category
      var seriesData = chartData.valueBeans.map(item => {
        return {
          name: '耗时(s)', type: 'line', data: item.data, itemStyle: {
            normal: {
              color: ['rgb(251, 110, 82)']
            }
          },
          areaStyle: {}
        }
      })

      return {
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          },
          formatter: function(params) {
            return 'cube名称:' + _this.chartData.originalDatas[params[0].axisIndex].jobCube + '<br/>' + '任务名称:' + params[0].name + '<br/>' + params[0].seriesName + ' : ' + params[0].value + 's'
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          boundaryGap: false,
          data: category,
          axisTick: {
            alignWithLabel: true
          },
          axisLabel: {
            show: false
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          },
          axisLabel: {
            formatter: '{value} (s)'
          }
        }],
        series: seriesData,
        animationDuration
      }
      /* [{
          name: 'pageA',
          type: 'bar',
          data: [79, 52, 200, 334, 390, 330, 220],
          animationDuration
        }] */
    }
  }
}
</script>
